package org.dstart.object.color.sub
{
	import org.dstart.csuper.SuperSprite;

	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.GradientType;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.ui.Mouse;

	public class ExColorListObject extends SuperSprite
	{
		public static const CLICK_COLOR:String	= "clickColor";		//	鼠标选择颜色

		private var m_root1:SuperSprite;	/** 大色块层 **/
		private var m_root2:SuperSprite;	/** 小色块层 **/
		
		private var m_cFlag1:Sprite;	/** 颜色选择标识1 **/
		private var m_cFlag2:Sprite		/** 颜色选择标识2 **/

		private var mSpr:Sprite;		/** 当前受鼠标控制的对象（决定调节颜色的对象） **/
		
		private var m_width:Number;		/** 填充区的宽度 **/
		private var m_height:Number;	/** 填充区的高度 **/
		
		public function ExColorListObject(numWidth:Number, numHeight:Number)
		{
			m_width = numWidth;
			m_height = numHeight;
			
			InitObject();
			InitListener();
		}
		
		/** 读取当前颜色值 */
		public function get color():uint
		{
			return this.m_root2._bmp.bitmapData.getPixel(5, m_cFlag2.y);
		}
		
		/** 绘制颜色 **/
		public function set color(col:uint):void
		{			
			this.DrawSelectColor(col);
			this.RushFlagPlace(col);
		}
		
		/** 初始化对象 **/
		private function InitObject():void
		{
			m_root1 = new SuperSprite();
			m_root2 = new SuperSprite();

			
			m_cFlag1 = new Sprite();
			m_cFlag2 = new Sprite();
			
			_root.addChild(m_root1);
			_root.addChild(m_root2);
			_root.addChild(m_cFlag1);
			_root.addChild(m_cFlag2);

			DrawBgColor();
			DrawColorFlag();
		}
		
		/** 设置基础事件侦听 **/
		private function InitListener():void
		{
			this.m_root1.addEventListener(MouseEvent.MOUSE_DOWN, onDownRoot1);
			this.m_root2.addEventListener(MouseEvent.MOUSE_DOWN, onDownRoot2);
			
			this.m_root1.addEventListener(MouseEvent.MOUSE_UP, onUpRoot);
			this.m_root1.addEventListener(MouseEvent.MOUSE_UP, onUpRoot);
		}
		
		/** 在大色块层里面调节 **/
		private function onDownRoot1(e:MouseEvent):void
		{
			mSpr = this.m_root1;
			
			onDownRoot(e);
		}
		
		/** 在小色块层时面调节 **/
		private function onDownRoot2(e:MouseEvent):void
		{
			mSpr = this.m_root2;
			
			onDownRoot(e);
		}
		
		/** 鼠标按下处理事件 **/
		private function onDownRoot(e:MouseEvent):void
		{			
			if(mSpr)
			{
				Mouse.hide();
				
				onMoveFlag(null);
				
				this.stage.addEventListener(MouseEvent.MOUSE_MOVE, onMoveFlag);
				this.stage.addEventListener(MouseEvent.MOUSE_UP, onUpRoot);
			}
		}
		
		/** 鼠标移动中事件 **/
		private function onMoveFlag(e:MouseEvent):void
		{
			var xx:Number = this.mouseX;
			var yy:Number = this.mouseY;
			var mc:Sprite;
			
			mc = (mSpr == m_root1) ? m_cFlag1 : m_cFlag2;
			
			mc.y = (yy < mSpr.y) ? mSpr.y : (yy > mSpr.y + mSpr.height - 1) ? mSpr.y + mSpr.height - 1 : yy;
			
			if(mSpr == m_root1)
			{
				mc.x = (xx < mSpr.x) ? mSpr.x : (xx > mSpr.x + mSpr.width - 1) ? mSpr.x + mSpr.width - 1 : xx;
				
				// trace(mc, mc.x, mc.y, this.m_root1._bmp.bitmapData.getPixel(mc.x, mc.y).toString(16));
				DrawSelectColor(this.m_root1._bmp.bitmapData.getPixel(mc.x, mc.y));
			}
						
			this.dispatchEvent(new Event(ExColorListObject.CLICK_COLOR));
		}
		
		/** 鼠标松开时事件 **/
		private function onUpRoot(e:MouseEvent):void
		{
			Mouse.show();
			
			mSpr = null;
			
			this.stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMoveFlag);
			this.stage.removeEventListener(MouseEvent.MOUSE_UP, onUpRoot);
		}		
		
		/** 绘制大颜色块 **/
		private function DrawBgColor():void
		{
			var colors:Array = new Array();
			var alphas:Array = new Array();
			var rations:Array = new Array();
			var matrix:Matrix;
			
			colors = new Array(0xFF0000, 0xFFFF00, 0x00FF00, 0x00FFFF, 0x0000FF, 0xFF00FF, 0xFF0000);
			alphas = new Array(100, 100, 100, 100, 100, 100, 100);
			rations = new Array(0, 42, 85, 127, 170, 212, 255);
			matrix = new Matrix();
			matrix.createGradientBox(m_width, m_height, 0, 0, 0);
			
			m_root1._root.graphics.beginGradientFill(GradientType.LINEAR, colors, alphas, rations, matrix);
			m_root1._root.graphics.drawRect(0, 0, m_width, m_height);
			
			colors = new Array(0x808080, 0x808080);
			alphas = new Array(0, 100);
			rations = new Array(0x00, 0xFF);
			matrix = new Matrix();
			matrix.createGradientBox(m_width, m_height, Math.PI/2, 0, 0);
			
			m_root1._root.graphics.beginGradientFill(GradientType.LINEAR, colors, alphas, rations, matrix);
			m_root1._root.graphics.drawRect(0, 0, m_width, m_height);
			m_root1._root.graphics.endFill();

			m_root1.OpenBitmap(m_width, m_height);			
		}
		
		/** 绘制色选择标识 **/
		private function DrawColorFlag():void
		{
			m_cFlag1.graphics.lineStyle(0, 0xFFFFFF, 1);			
			m_cFlag1.graphics.moveTo(1, 0);
			m_cFlag1.graphics.lineTo(4, 0);
			m_cFlag1.graphics.moveTo(-1, 0);
			m_cFlag1.graphics.lineTo(-4, 0);
			m_cFlag1.graphics.moveTo(0, 1);
			m_cFlag1.graphics.lineTo(0, 4);
			m_cFlag1.graphics.moveTo(0, -1);
			m_cFlag1.graphics.lineTo(0, -4);	
			
			m_cFlag2.graphics.beginFill(0x000000, 1);
			m_cFlag2.graphics.moveTo(0, 0);
			m_cFlag2.graphics.lineTo(6, -3);
			m_cFlag2.graphics.lineTo(6, 3);
			m_cFlag2.graphics.lineTo(0, 0);		
		}
		
		/** 绘制一种颜色渐变的竖条（颜色直接由它决定） **/
		private function DrawSelectColor(col:uint):void
		{
			var colors:Array = new Array(0xFFFFFF, 0xFFFFFF, 0x000000, 0x000000);
			var alphas:Array = new Array(100, 0, 0, 100);
			var rations:Array = new Array(0, 127, 128, 255);
			var matrix:Matrix = new Matrix();
			
			matrix.createGradientBox(10, m_height, Math.PI/2, 0, 0);
			
			m_root2._root.graphics.clear();
			m_root2._root.graphics.beginFill(col, 1);
			m_root2._root.graphics.drawRect(0, 0, 10, m_height);
			
			m_root2._root.graphics.beginGradientFill(GradientType.LINEAR, colors, alphas, rations, matrix);
			m_root2._root.graphics.drawRect(0, 0, 10, m_height);
			m_root2._root.graphics.endFill();
			
			m_root2.OpenBitmap(10, m_height);
						
			m_root2.x = m_root1.x + m_root1.width + 4;
			m_cFlag2.x = m_root2.x + m_root2.width + 1;
		}
		
		/** 刷新颜色标识所在的位置 **/
		private function RushFlagPlace(col:uint):void
		{
			var cStr:String = ("000000" + col.toString(16)).substr(-6);
			var rr:Number = Number("0x" + cStr.substr(0, 2));
			var gg:Number = Number("0x" + cStr.substr(2, 2));
			var bb:Number = Number("0x" + cStr.substr(4, 2));
			var hsl:Object = rgbToHsl(rr, gg, bb);
			
			m_cFlag1.x = Math.round(hsl.h * (m_width - 1)/359) + 1;
			m_cFlag1.y = m_height - Math.round(hsl.s * m_height);
			m_cFlag2.y = m_height - Math.round(hsl.l * m_height);
		}
		
		/** 取得 HSL 方式的颜色值 **/
		private function rgbToHsl(R:Number, G:Number, B:Number):Object 
		{
			var max:Number;
			var min:Number;
			var diff:Number;
			var r_dist:Number;
			var g_dist:Number;
			var b_dist:Number;
			var hsl:Object = new Object();

			R /= 255;
			G /= 255;
			B /= 255;
			
			max = Math.max(Math.max(R, G), B);
			min = Math.min(Math.min(R, G), B);
			diff = max-min;
			hsl.l = (max+min)/2;
			
			if (diff == 0) 
			{
				hsl.h = 0;
				hsl.s = 0;
			}
			else 
			{
				if (hsl.l<0.5) 
				{
					hsl.s = diff/(max+min);
				}
				else 
				{
					hsl.s = diff/(2-max-min);
				}
				
				r_dist = (max-R)/diff;
				g_dist = (max-G)/diff;
				b_dist = (max-B)/diff;
				
				if (R == max) 
				{
					hsl.h = b_dist-g_dist;
				}
				else if (G == max) 
				{
					hsl.h = 2+r_dist-b_dist;
				}
				else if (B == max) 
				{
					hsl.h = 4+g_dist-r_dist;
				}
				
				hsl.h *= 60;
				
				if (hsl.h<0) 
				{
					hsl.h += 360;
				}
				if (hsl.h>=360) 
				{
					hsl.h -= 360;
				}
			}
			
			return hsl;
		}
	}
}